#首页
from selenium.webdriver.common.by import By
from base.base import Base
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException
import time
import re
class PageHome(Base):
    title_locator = (By.XPATH,'//div[@class="ele-admin-logo"]/span')
    topnav_locator = (By.XPATH,'//li[@role="menuitem"]')
    system_bt_locator = (By.XPATH, '//span[text()="系统管理"]')
    #所有菜单选择器
    all_nav_locator = (By.XPATH,'//span[@class="ant-menu-title-content"]/span')
    #设备统计
    count_top_locator = (By.CLASS_NAME,'analysis-chart-card-num')
    count_bottom_locator = (By.CLASS_NAME,'analysis-trend-text')
    #告警数据
    alar_locator = (By.XPATH,'//tbody//*[contains(@class,"ant-table-cell")]')
    #站点统计在线、离线等数据
    count_sites_locator = (By.XPATH,'//label')
    #站点数量
    sitenumber_locator = (By.XPATH,'//div[@class="ant-space-item"]//label')
    site_locator = (By.XPATH,'//div[contains(@class,"scene-item")]')
    #用户名称
    username_locator = (By.XPATH,'//span[@class="hidden-sm-and-down"]')
    #个人中心按钮
    personcenter_locator = (By.XPATH,'//div[text()="个人中心"]')
    #基本信息
    baseinfo_locator = (By.XPATH,'//div[@class="ant-form-item-control-input-content"]')
    #角色信息
    roleinfo_locator = (By.XPATH,'//span[@class="ant-tag ant-tag-blue"]')
    #获取标题信息
    def get_title_message(self):
        return self.find(self.title_locator).text
    #点击顶部菜单
    def get_topnav(self):
        values = []
        for elm in self.finds(self.topnav_locator):
            elm.click()
            values.append(elm.text)
            time.sleep(0.1)
        return values
    #点击子菜单
    def get_sbumenu(self):
        system_menu = self.find(self.system_bt_locator)
        ActionChains(self.driver).move_to_element(system_menu).perform()
        elms = self.presences(self.all_nav_locator)
        values = []
        for index,elm in enumerate(elms):
            if index > 7:
                values.append(elm.text)
                system_menu = self.find(self.system_bt_locator)
                ActionChains(self.driver).move_to_element(system_menu).perform()
                ActionChains(self.driver).move_to_element(elm).click().perform()
                time.sleep(0.1)
        return values
    #点击标题跳转
    def title_jump(self):
        self.click(self.title_locator)
    #个人中心
    def get_personcenter(self):
        ActionChains(self.driver).move_to_element(self.find(self.username_locator)).perform()
        ActionChains(self.driver).click(self.find(self.personcenter_locator)).perform()
        info_values = []
        for elm in self.finds(self.baseinfo_locator):
            info_values.append(elm.text)
        for elm in self.finds(self.roleinfo_locator):
            info_values.append(elm.text)
        return info_values
    #获取顶部统计数据
    def get_countvalues(self):
        values = []
        newvalues = []
        for elm in self.finds(self.count_top_locator):
            values.append(elm.text)
        for elm in self.finds(self.count_bottom_locator):
            values.append(elm.text)
        for item in values:
            nums = re.findall(r'\d+', item)  # 找到所有数字
            newvalues.extend(map(int,nums))        
        return(newvalues)
    #获取告警列表数据
    def get_alarvalues(self):
        values = []
        for elm in self.finds(self.alar_locator):
            values.append(elm.text)
        return values
    def get_sitenumber(self):
        values = []
        for elm in self.finds(self.sitenumber_locator):
            nums = re.findall(r'\d+', elm.text)  # 找到所有数字
            values.extend(map(int,nums))    
        return(values)
    #获取所有站点信息
    def get_countsite(self):
        values = []
        newvalues = []
        for elm in self.finds(self.count_sites_locator):
            elm.click()  
            try:
                sites = self.finds(self.site_locator)
                for site in sites:
                    values.append(site.text)
            except TimeoutException:
                continue
        for value in values:
            newvalue = value.split('\n')
            for index,key in enumerate(newvalue):
                if index%11 in [0,1,2,3,5,7,9]:
                    newvalues.append(key)
        return newvalues
